﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PowerTest.GiaoVien
{
    public partial class frmTaoCauHoi : Form
    {
        public frmTaoCauHoi()
        {
            InitializeComponent();
        }

        private void frmTaoCauHoi_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'powerTestDataSet.viewCauHoiMucDo' table. You can move, or remove it, as needed.
            this.viewCauHoiMucDoTableAdapter.Fill(this.powerTestDataSet.viewCauHoiMucDo);
            
            // TODO: This line of code loads data into the 'powerTestDataSet.tblMucDo' table. You can move, or remove it, as needed.
            this.tblMucDoTableAdapter.Fill(this.powerTestDataSet.tblMucDo);
            // TODO: This line of code loads data into the 'powerTestDataSet.tblMonHoc' table. You can move, or remove it, as needed.
            this.tblMonHocTableAdapter.Fill(this.powerTestDataSet.tblMonHoc);
            // TODO: This line of code loads data into the 'powerTestDataSet.viewTaoCauHoi' table. You can move, or remove it, as needed.
            this.viewTaoCauHoiTableAdapter.Fill(this.powerTestDataSet.viewTaoCauHoi);
            
            this.tblCauHoiTableAdapter.Fill(powerTestDataSet.tblCauHoi);
            lblTenGV.Text = Func.tenTruyCap;
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            try
            {
                int i = 1;
                tblDapAnNhapBindingSource.RemoveCurrent();
                foreach (DataGridViewRow rnhap in dgvNhapDapAn.Rows)
                {
                    rnhap.Cells[0].Value = i;
                    i++;
                }
            }
            catch
            {
                //MessageBox.Show("Lỗi!");
            }
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                int i = 1;
                tblDapAnNhapBindingSource.AddNew();
                foreach (DataGridViewRow rnhap in dgvNhapDapAn.Rows)
                {
                    rnhap.Cells[0].Value = i;
                    i++;
                }
                dgvNhapDapAn.Rows[dgvNhapDapAn.RowCount - 1 ].Cells[1].Selected = true;
                dgvNhapDapAn.BeginEdit(true);
                //dgvNhapDapAn.Rows[dgvNhapDapAn.RowCount - 1].Cells[1].f
            }
            catch { MessageBox.Show("Lỗi!"); }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            gbMon.Enabled = false;
            gbCauHoi.Enabled = true;
            txtCauHoi.Focus();
        }

        private void btXacNhanCauHoi_Click(object sender, EventArgs e)
        {
            gbCauHoi.Enabled = false;
            gbDapAn.Enabled = true;
            btThem.Focus();
        }

        private void btQuayLaiHoi_Click(object sender, EventArgs e)
        {
            gbDapAn.Enabled = false;
            gbCauHoi.Enabled = true;
        }

        private void btQuayLai_Click(object sender, EventArgs e)
        {
            gbDapAn.Enabled = true;
            btQuayLai.Enabled = false;
            btXacNhan.Enabled = false;
            btLuuCauHoiVaoCSDL.Enabled = false;

        }

        private void btXacNhan_Click(object sender, EventArgs e)
        {
            btLuuCauHoiVaoCSDL.Enabled = true;
            btXacNhan.Enabled = false;
            lblThongBao.Text = "Chọn Lưu câu hỏi để nhập câu hỏi vào CSDL";
        }

        private void btXacNhanDapAn_Click(object sender, EventArgs e)
        {
            if (dgvNhapDapAn.RowCount > 0)
            {
                gbDapAn.Enabled = false;
                btQuayLai.Enabled = true;
                btXacNhan.Enabled = true;
                lblThongBao.ForeColor = Color.Black;
                lblThongBao.Text = "Thông báo tình trạng";
            }
            else
            {
                lblThongBao.Text = "Chưa có đáp án nào tương ứng với câu hỏi ở trên!";
                lblThongBao.ForeColor = Color.Red;
            }
        }

        private void btQuayLaiMon_Click(object sender, EventArgs e)
        {
            gbCauHoi.Enabled = false;
            gbMon.Enabled = true;
        }

        private void btLuuCauHoiVaoCSDL_Click(object sender, EventArgs e)
        {
            string maMon = cbMon.SelectedValue.ToString();
            string maMucDo = cbMucDo.SelectedValue.ToString();
            string maCauHoi = Func.autoMCH();
            string dapAnDung, dapAn;
            int MDA =0;

            if (dgvNhapDapAn.RowCount > 0)
            {
                tblCauHoiTableAdapter.Insert(maCauHoi, maMon, txtCauHoi.Text, maMucDo, Func.maTruyCap);

                foreach (DataGridViewRow row in dgvNhapDapAn.Rows)
                {
                    try
                    {
                        MDA ++;
                        if (row.Cells["dungDataGridViewCheckBoxColumn"].Value.ToString() == "True")
                        {
                            dapAnDung = "True";
                        }
                        else
                        {
                            dapAnDung = "False";
                        }
                        dapAn = row.Cells["nhapdapanDataGridViewTextBoxColumn"].Value.ToString();
                        tblChiTietDapAnTableAdapter.Insert(maCauHoi, MDA.ToString(), dapAn, dapAnDung);
                    }
                    catch //(Exception ex)
                    {
                        MessageBox.Show("Lỗi! Nhập");//ex.ToString());
                    }
                }
                this.tblCauHoiTableAdapter.FillByMaMon(powerTestDataSet.tblCauHoi,cbMon.SelectedValue.ToString());
                lblThongBao.Text = "Lưu dữ liệu hoàn thành";
                lblThongBao.ForeColor = Color.Blue;
                btNhapThem.Enabled = true;
                btQuayLai.Enabled = false;
                btLuuCauHoiVaoCSDL.Enabled = false;
            }
            else
            {
                MessageBox.Show("Chưa có dữ liệu về đáp án");
            }
        }

        private void dgvCauHoi_SelectionChanged(object sender, EventArgs e)
        {
            if ( dgvCauHoi.CurrentRow == null)
            {
                return;
            }
            try 
            {
                string maCauHoi = dgvCauHoi.CurrentRow.Cells["maCauHoiDataGridViewTextBoxColumn"].Value.ToString();
                tblChiTietDapAnTableAdapter.FillByByMCH(powerTestDataSet.tblChiTietDapAn,maCauHoi);
            }
            catch
            {
            }
        }

        private void btNhapThem_Click(object sender, EventArgs e)
        {
            /*gbCauHoi.Enabled = true;
            txtCauHoi.Text = "Nhập Câu Hỏi???";
            txtCauHoi.Focus();
            btNhapThem.Enabled = false;
            dgvNhapDapAn.DataSource = null;*/
            //Close();
            Dispose();
            new frmTaoCauHoi().ShowDialog();
        }

        private void cbMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                viewCauHoiMucDoTableAdapter.FillByMaMon(powerTestDataSet.viewCauHoiMucDo, cbMon.SelectedValue.ToString());
                this.tblCauHoiTableAdapter.FillByMaMon(powerTestDataSet.tblCauHoi, cbMon.SelectedValue.ToString());
            }
            catch { }
        }

    }
}